*
* Table 5: Program characteristics and dominated choices
*
* Input table: 
*	"${data_applicant_level_tidy}"
*
* Output file: 
*	/results/Table5.tex
*

use "${data_applicant_level_tidy}", clear

keep female age sec_school voc_school capital countycapital town village ///
	GPA11 GPA11_missing disadv sample_hss contract_num contract_num_data ///
	program_num_data ROL_state_funded_only ROL_state_self_funded ROL_self_funded_only ///
	winner_lower loser_lower winner_upper loser_upper year

cap file close sumstat
file open sumstat using "${result_dir}/Table5.tex", write replace
file write sumstat "\begin{tabular}{l c c c c c c c} \\ \hline\hline" _n
file write sumstat " & \multicolumn{3}{c}{State-funded above self-funded} & &  \multicolumn{3}{c}{State-funded on top} \\ \cline{2-4} \cline{6-8} " _n
file write sumstat " & \multicolumn{1}{c}{Winners} & \multicolumn{1}{c}{Losers} & p-values: (1)=(2) & & \multicolumn{1}{c}{Winners} & \multicolumn{1}{c}{Losers} & p-values: (4)=(5) \\" _n
file write sumstat " & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} & \multicolumn{1}{c}{(6)} \\ \hline" _n

foreach var of varlist female age {
		local lab_var: variable label `var'
	
		qui sum `var' if winner_lower == 1
		local mean_w_l_`var': display %4.2f r(mean)
		
		qui sum `var' if loser_lower == 1
		local mean_l_l_`var': display %4.2f r(mean)
		
		reg `var' winner_lower i.year if winner_lower == 1 | loser_lower == 1, r
		local diff_lower_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_lower]/_se[winner_lower])))
		
		qui sum `var' if winner_upper == 1
		local mean_w_u_`var': display %4.2f r(mean)
		local sd_w_u_`var': display %4.2f r(sd)
		
		qui sum `var' if loser_upper == 1
		local mean_l_u_`var': display %4.2f r(mean)
		local sd_l_u_`var': display %4.2f r(sd)
		
		qui reg `var' winner_upper i.year if winner_upper == 1 | loser_upper == 1, r
		local diff_upper_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_upper]/_se[winner_upper])))
		
		file write sumstat " `lab_var' & `mean_w_l_`var'' & `mean_l_l_`var'' & `diff_lower_p' & & `mean_w_u_`var'' & `mean_l_u_`var'' &  `diff_upper_p' \\ " _n

}
file write sumstat "High school & & \\ " _n
foreach var of varlist sec_school voc_school {
		local lab_var: variable label `var'
	
		qui sum `var' if winner_lower == 1
		local mean_w_l_`var': display %4.2f r(mean)
		local sd_w_l_`var': display %4.2f r(sd)
		
		qui sum `var' if loser_lower == 1
		local mean_l_l_`var': display %4.2f r(mean)
		local sd_l_l_`var': display %4.2f r(sd)
		
		reg `var' winner_lower i.year if winner_lower == 1 | loser_lower == 1, r
		local diff_lower_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_lower]/_se[winner_lower])))
		
		qui sum `var' if winner_upper == 1
		local mean_w_u_`var': display %4.2f r(mean)
		local sd_w_u_`var': display %4.2f r(sd)
		
		qui sum `var' if loser_upper == 1
		local mean_l_u_`var': display %4.2f r(mean)
		local sd_l_u_`var': display %4.2f r(sd)
		
		qui reg `var' winner_upper i.year if winner_upper == 1 | loser_upper == 1, r
		local diff_upper_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_upper]/_se[winner_upper])))
		
		file write sumstat " `lab_var' & `mean_w_l_`var'' & `mean_l_l_`var'' & `diff_lower_p' & & `mean_w_u_`var'' & `mean_l_u_`var'' &  `diff_upper_p' \\ " _n

}	
file write sumstat "Residence & & & & & & & \\ " _n
foreach var of varlist capital countycapital town village GPA11 GPA11_missing disadv sample_hss contract_num contract_num_data program_num_data {
		local lab_var: variable label `var'
	
		qui sum `var' if winner_lower == 1
		local mean_w_l_`var': display %4.2f r(mean)
		local sd_w_l_`var': display %4.2f r(sd)
		
		qui sum `var' if loser_lower == 1
		local mean_l_l_`var': display %4.2f r(mean)
		local sd_l_l_`var': display %4.2f r(sd)
		
		reg `var' winner_lower i.year if winner_lower == 1 | loser_lower == 1, r
		local diff_lower_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_lower]/_se[winner_lower])))
		
		qui sum `var' if winner_upper == 1
		local mean_w_u_`var': display %4.2f r(mean)
		local sd_w_u_`var': display %4.2f r(sd)
		
		qui sum `var' if loser_upper == 1
		local mean_l_u_`var': display %4.2f r(mean)
		local sd_l_u_`var': display %4.2f r(sd)
		
		qui reg `var' winner_upper i.year if winner_upper == 1 | loser_upper == 1, r
		local diff_upper_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_upper]/_se[winner_upper])))
		
		file write sumstat " `lab_var' & `mean_w_l_`var'' & `mean_l_l_`var'' & `diff_lower_p' & & `mean_w_u_`var'' & `mean_l_u_`var'' &  `diff_upper_p' \\ " _n

}
file write sumstat "ROL by funding type  & & & & & & & \\ " _n
foreach var of varlist ROL_state_funded_only ROL_state_self_funded ROL_self_funded_only {
		local lab_var: variable label `var'
	
		qui sum `var' if winner_lower == 1
		local mean_w_l_`var': display %4.2f r(mean)
		local sd_w_l_`var': display %4.2f r(sd)
		
		qui sum `var' if loser_lower == 1
		local mean_l_l_`var': display %4.2f r(mean)
		local sd_l_l_`var': display %4.2f r(sd)
		
		reg `var' winner_lower i.year if winner_lower == 1 | loser_lower == 1, r
		local diff_lower_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_lower]/_se[winner_lower])))
		
		qui sum `var' if winner_upper == 1
		local mean_w_u_`var': display %4.2f r(mean)
		local sd_w_u_`var': display %4.2f r(sd)
		
		qui sum `var' if loser_upper == 1
		local mean_l_u_`var': display %4.2f r(mean)
		local sd_l_u_`var': display %4.2f r(sd)
		
		qui reg `var' winner_upper i.year if winner_upper == 1 | loser_upper == 1, r
		local diff_upper_p: display %4.3f (2 * ttail(e(df_r), abs(_b[winner_upper]/_se[winner_upper])))
		
		file write sumstat " `lab_var' & `mean_w_l_`var'' & `mean_l_l_`var'' & `diff_lower_p' & & `mean_w_u_`var'' & `mean_l_u_`var'' &  `diff_upper_p' \\ " _n

}

	qui sum contract_num if winner_lower == 1
	local n_obs_w_l: display %12.0gc r(N)
		
	qui sum contract_num if loser_lower == 1
	local n_obs_l_l: display %12.0gc r(N)
		
	qui sum contract_num if winner_upper == 1
	local n_obs_w_u: display %12.0gc r(N)
			
	qui sum contract_num if loser_upper == 1
	local n_obs_l_u: display %12.0gc r(N)
	
file write sumstat "Number of applicants & `n_obs_w_l' & `n_obs_l_l' & & & `n_obs_w_u' & `n_obs_l_u' & \\ \hline\hline " _n
file write sumstat "\end{tabular}" _n
file close sumstat
